//
//  MBSpaceSearchDataViewController.m
//  MiBaoTu
//
//  Created by admin on 16/11/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceSearchDataViewController.h"
#import "MBChildRecordForIController.h"
#import "MBChildRecordController.h"

#import "MBRecordCell.h"

@interface MBSpaceSearchDataViewController ()<UITableViewDataSource,UITableViewDelegate,MBRecordCellDelegate> {
    UITableView                     *_tableView;
    NSMutableArray                  *_mutArr;
    int                             indexCount;
}

@end
@implementation MBSpaceSearchDataViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _mutArr = [[NSMutableArray alloc]init];
    indexCount = 1;
    //this is a xxxx
    
    [self onBackItem];
    
    [self onCreateUI];
}

- (void)onBackItem
{
    [self setTitleView:@"搜索结果"];
    
    [self setBackItem];
    
    [self setRightPlayerItem];
}

- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
    [_tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    
    [self onLoadNew];
}

#pragma mark - 数据加载
- (void)onLoadNew
{
    indexCount = 1;
    [self onLoadData];
}

- (void)onLoadMore
{
    ++indexCount;
    [self onLoadData];
}

- (void)onLoadData
{
    NSString *url = [NSString stringWithFormat:FAMILYSHOW_RECORD_LIST_URL_PATH,[NSString stringWithFormat:@"%d",indexCount],@1,_textString];

    [MBProgressHUD showHUDAddedTo:_tableView animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [MBProgressHUD hideHUDForView:_tableView animated:YES];
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    if (indexCount == 1) {
                        [_mutArr removeAllObjects];
                    }
                    
                    [_mutArr addObjectsFromArray:arr];
                }
                [_tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [MBProgressHUD hideHUDForView:_tableView animated:YES];
    }];
    

}

#pragma mark - 录制
- (void)onSelectItemForRecordQinZiXiu:(id)value
{
    if ([value isKindOfClass:[NSDictionary class]]) {
        NSString *lrcPath = [value objectForKey:@"Content"];
        
        if (ISIPAD) {
            MBChildRecordForIController *childRecordVC = [[MBChildRecordForIController alloc]init];
            childRecordVC.lrcString = lrcPath;
            childRecordVC.dataDic = value;
            [self.navigationController pushViewController:childRecordVC animated:YES];
        }else{
            MBChildRecordController *childRecordVC = [[MBChildRecordController alloc]init];
            childRecordVC.lrcString = lrcPath;
            childRecordVC.dataDic = value;
            [self.navigationController pushViewController:childRecordVC animated:YES];
        }
        
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identify = @"MBRecordCell";
    MBRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MBRecordCell" owner:self options:nil] lastObject];
        cell.delegate = self;
    }
    
    if (ISIPAD) {
        
        cell.imgLeft.constant = 440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        cell.recordRight.constant = 440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        cell.lineViewLeft.constant = 440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        cell.lineViewRight.constant = 440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        cell.lineView.backgroundColor = [UIColor lightGrayColor];
        
    }
    
    if (_mutArr.count > indexPath.row) {
        cell.dateDic = [_mutArr objectAtIndex:indexPath.row];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_mutArr.count > indexPath.row) {
        
        NSDictionary *value = [_mutArr objectAtIndex:indexPath.row];
        
        if ([value isKindOfClass:[NSDictionary class]]) {
            NSString *lrcString = [value objectForKey:@"Content"];
            
            if (ISIPAD) {
                MBChildRecordForIController *childRecordVC = [[MBChildRecordForIController alloc]init];
                childRecordVC.lrcString = lrcString;
                childRecordVC.dataDic = value;
                [self.navigationController pushViewController:childRecordVC animated:YES];
            }else{
                MBChildRecordController *childRecordVC = [[MBChildRecordController alloc]init];
                childRecordVC.lrcString = lrcString;
                childRecordVC.dataDic = value;
                [self.navigationController pushViewController:childRecordVC animated:YES];
            }
            
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end
